<?php

namespace App\Http\Validators\V1;

use App\Helpers\Utils;
use App\Http\Validators\AbstractValidator;

/**
 * @author zhaoqiying
 */
class LoginValidator extends AbstractValidator
{

    /**
     * Validation rules
     *
     * @var Array
     */
    protected $rules = array(
        'phone'   => ['required', 'is_phone'],

    );

    /**
     * Validation messages
     *
     * @var Array
     */
    protected $messages = array(
        'phone.required' => '请输入手机号',
        'phone.is_phone' => '手机号格式错误',

    );

    /**
     * Validation codes
     *
     * @var Array
     */
    public $codes = array(
        'keywords.required' => '1001',
        'keywords.is_username' => '1002',
        'keywords.is_not_email' => '1003',
        'password.required' => '1004',
        'password.alpha_num' => '1005',
        'password.size' => '1006',
    );

    /*
     * 自定义验证规则或者扩展Validator类
     */

    public function before()
    {
        // 自定义规则检查用户名
//        $this->extend('is_username', function($attribute, $value, $parameters)
//        {
//            if (preg_match("/^[0][8]/d{7,10}$/u", $value))
//            {
//                $strLen = Utils::utf8StrLen($value);
//                if ($strLen <= 16 && $strLen >= 4)
//                {
//                    return true;
//                }
//            }
//            return false;
//        });

        // 移动端不支持邮箱登陆
//        $this->extend('is_not_email', function($attribute, $value, $parameters)
//        {
//            return !filter_var($value, FILTER_VALIDATE_EMAIL);
//        });
	    
	    //自定义规则检查用户手机号
	    $this->extend('is_phone',function($attribute,$value,$paramters){
            if (preg_match('/^1[3|4|5|7|8]\d{9}$/', $value)){
	    		return true;
		    } else {
		    	return false;
		    }
	    });
    }

}
